"""
Case Type   : 小型化
Case Name   : gs_restore时，使用--pipeline参数，不使用echo参数
Create At   : 2025/1/22
Owner       : wan005
Description :
    1.准备数据
    2.gs_dump导出数据
    3.gs_restore导入数据，使用--pipeline参数
Expect      :
    1.数据准备完成
    2.导出成功
    3.导入失败
History     :
"""
import os
import unittest
from testcase.utils.ComInstall import LiteInstall, LiteUnInstall
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class Lite(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.com_install = LiteInstall(self.pkg_path, None, None)
        self.com_uninstall = LiteUnInstall(self.pkg_path)
        self.root_node = Node('PrimaryRoot')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.user = None
        self.port = None
        self.table = 't_lite_001'

    def test_audit(self):
        text = '----prestep1:获取可用用户和端口;expect:获取成功----'
        self.log.info(text)
        self.user, self.port = self.com_install.get_user_and_port(self.root_node)
        self.log.info(f'self.user:{self.user}, self.port:{self.port}')
        self.user_node.ssh_user = self.user
        self.user_node.ssh_password = macro.COMMON_PASSWD
        self.data_path = os.path.join('/home', self.user, 'data')
        self.app_path = os.path.join('/home', self.user, 'app')
        self.audit_path = os.path.join(self.data_path, 'pg_audit')
        self.com_install.data_path = self.data_path
        self.com_install.app_path = self.app_path

        text = '----prestep2:创建数据库用户和用户组;expect:创建成功----'
        self.log.info(text)
        self.com_install.prepare_user(self.user, self.root_node)

        text = '----prestep3:获取数据库安装包并解压;expect:成功----'
        self.log.info(text)
        res = self.com_install.tar_pkg(self.root_node)
        self.assertIn('install.sh', res, f'执行失败: {text}')

        text = '----prestep4:执行脚本安装数据库;expect:安装成功----'
        self.log.info(text)
        res = self.com_install.install_single(self.user_node, self.port)
        self.log.info(res)
        self.assertIn('start datanode success', res, f'执行失败: {text}')

        text = '----step1:准备数据;expect:成功---'
        self.log.info(text)
        sql_cmd1 = f'create table {self.table} (id int);' \
                  f'insert into {self.table} values(generate_series(1,10));'
        exe_cmd1 = f'gsql -d postgres -p {self.port} -c "{sql_cmd1}"'
        self.log.info(exe_cmd1)
        res1 = self.user_node.sh(exe_cmd1).result()
        self.log.info(res1)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, res1, '执行失败:' + text)

        text = '----step2:gs_dump导出数据;expect:成功----'
        self.log.info(text)
        exe_cmd2 = f'''expect <<EOF
                    set timeout 10
                    spawn gs_dump -p {self.port} postgres -f \
                    {os.path.join(self.app_path, "test.sql")} 
                    expect eof\n''' + '''EOF'''
        self.log.info(exe_cmd2)
        res2 = self.user_node.sh(exe_cmd2).result()
        self.log.info(res2)
        self.assertIn('dump database postgres successfully', res2,
                      '执行失败:' + text)

        text = '----step3:gs_restore导入数据,使用--pipeline参数;expect:失败----'
        self.log.info(text)
        exe_cmd3 = f'''expect <<EOF
                    set timeout 10
                    spawn gs_restore -d postgres \
                    {os.path.join(self.app_path, "test.sql")} --pipeline
                    expect eof\n''' + '''EOF'''
        self.log.info(exe_cmd3)
        res3 = self.user_node.sh(exe_cmd3).result()
        self.log.info(res3)
        self.assertIn('Terminal is not allowed to use --pipeline', res3,
                      '执行失败:' + text)

    def tearDown(self):
        text = '-----清理环境-----'
        self.log.info(text)
        res = self.com_uninstall.uninstall(self.user_node)
        self.com_uninstall.clear_env(self.root_node)
        self.assertIn('success', res, f'执行失败: {text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
